<template>
	<view class="">
		<form @submit="formSubmit">
			<view class="list">
				<!-- 店铺照片/视频 -->
				<view class="module">
					<view class="acea-row row-middle row-between">
						<view class="module-title">
							店铺照片/视频
						</view>
					</view>
					<view class="example-text-l">
						请至少添加一张铺面正面照
					</view>
					<view class="acea-row pictrue-list">
						<view class="acea-row row-column row-center-wrapper pictrue"
							v-for="(item,index) in shopfrontPhotoList" :key="index" @click="deleteImage(index)">
							<image style="width: 218rpx;height: 172rpx;" :src="item" mode=""></image>
						</view>
						<view class="acea-row row-column row-center-wrapper pictrue" @click="selectPicture"
							v-if="!shopfrontPhotoList.length<2">
							<view class="upload-icon">
								<image src="../static/images/upload_icon.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<!-- 				<view class="acea-row row-middle row-between item border-b ">
					<view class="label">
						店铺名称
					</view>
					<view class="acea-row row-middle row-right item-r">
						<input class="input-r" type="text" name="storeName" placeholder="请输入店铺名称"
							placeholder-class="placeholder-class" v-model="dataform.storeName" />
					</view>
				</view> -->

				<!-- 联系人 -->
				<view class="acea-row row-middle row-between item  ">
					<view class="label label-m">
						联系人
					</view>
					<view class="add-icon" @click="addContact">
						<image src="../static/images/add_icon.png" mode=""></image>
					</view>
				</view>
				<view class="item border-b">
					<chooseContact :list="contactList" :dataform="dataform" @add="d=>contactList.push(d)"
						@del="toDel" />
				</view>

			</view>

			<!-- 按钮 -->
			<view class="acea-row row-between row-middle btns">
				<button class="red-btn" style="width: 100%;" @click="formSubmit(2)">提交</button>
			</view>
		</form>
		<ImageInstance ref="ImageInstance"></ImageInstance>
		<u-loading-page loading-text="保存中..." :loading="uload"></u-loading-page>
	</view>
</template>

<script>
	import {
		ShopRentDetailApiNoDw,
		editInfo,
		aiPhotoApi
	} from '@/api/subletOfShop.js'
	import {
		Debounce
	} from '@/utils/validate.js'
	import {
		informationAdd
	} from "@/api/subletOfShop.js"
	import {
		toPage,
		getCategory
	} from '@/filters/commFilter.js'
	import ImageInstance from '../components/ImageInstance.vue'

	import chooseContact from '../components/chooseContact.vue'
	export default {
		components: {
			ImageInstance,
			chooseContact
		},
		props: ['categoryOn', 'typeOn', 'editId', 'patientinfo', 'st', 'storeCategory'],
		data() {
			return {
				shopfrontPhotoList: [],
				storeAdvantage: [],
				contactList: [],
				index: 0,
				uload: false,
				dataform: {
					businessCircle: '',
					businessName: '',
				},
				sts: 0
			}
		},
		mounted() {
			let that = this
			that.dataform.addres = that.$address;
			that.$set(that.dataform, 'latitude', this.$location.latitude)
			that.$set(that.dataform, 'longitude', this.$location.longitude)
			that.dataform.status = that.typeOn
			that.dataform.storeCategory = getCategory(this.categoryOn).value;
			this.$nextTick(() => {
				that.getBusiness(that.$location)
			})
			this.$forceUpdate()

			getApp().globalData.initContact()
			uni.$on('means', function(data) {
				that.dataform = {
					...that.dataform,
					...data
				};
				that.formSubmit()
			})
			// console.log(that.dataform.storeCategory,this.storeCategory, this.typeOn);
			//先默认赋予当前
			this.dataform.city = this.$cityCode ? this.$cityCode[1] : '';
			this.dataform.province = this.$cityCode ? this.$cityCode[0] : '';
			this.dataform.district = this.$cityCode ? this.$cityCode[2] : '';
		},
		methods: {
			toPage,
			addConfirm(e) {
				this.advantages = this.advantages.concat(e)
			},
			toDel(index, info, callback) {
				if (info) {
					this.contactList.splice(index, 1, info);
				} else {
					this.contactList.splice(index, 1);
				}
				callback && callback();
			},
			//查看图片示例
			getImage() {
				this.$refs.ImageInstance.show = true
			},
			selectPicture() {
				let that = this;
				that.$util.uploadImageOne({
					url: 'upload/image',
					count: 1,
					name: 'multipart',
					model: "user",
					pid: 0,
				}, function(res) {
					console.log("shangchaunres", res)
					that.shopfrontPhotoList.push(res.data.url)
				});
			},
			getBusiness(location) {
				let that = this;
				getApp().globalData.getBusinessList(location).then(res => {
					if (res.code == 200) {
						console.log('res', res)
						that.dataform.province = res.data.province;
						that.dataform.city = res.data.city;
						that.dataform.district = res.data.district;
						that.dataform.businessName = res.data.businessName;
						that.dataform.businessCircle = res.data.businessId;
					}
				});
			},
			deleteImage(index) {
				this.shopfrontPhotoList.splice(index, 1)
			},

			async formSubmit(status) {
				if (this.shopfrontPhotoList.length < 1) {
					this.$util.Tips({
						title: '请上传店铺图片'
					})
					return;
				}

				this.dataform.contacts = this.contactList.map(item => {
					const {
						id,
						type,
						open,
						were
					} = item;
					return {
						contactId: id,
						identity: type,
						open,
						were
					}
				})
				console.log(this.dataform)
				this.dataform.imgSrc = this.shopfrontPhotoList[0];
				this.uload = true;
				aiPhotoApi(this.dataform).then(res => {
					this.uload = false;
					this.$util.Tips({
						title: '店铺添加成功'
					})
					this.dataform.imgSrc = '';
					this.contactList=[];
				}).catch(e => {
					this.uload = false;
				})
			},

			addContact() {
				uni.navigateTo({
					url: '/pages/releases/addShopContact/addShopContact'
				})
			},
			treeConfirm(data) {
				console.log(data)
			},
		},


	}
</script>

<style lang="scss">
	@import '/static/css/releaseForm.css';
</style>